<!--
  -    Copyright (c) 2018-2025, lengleng All rights reserved.
  -
  - Redistribution and use in source and binary forms, with or without
  - modification, are permitted provided that the following conditions are met:
  -
  - Redistributions of source code must retain the above copyright notice,
  - this list of conditions and the following disclaimer.
  - Redistributions in binary form must reproduce the above copyright
  - notice, this list of conditions and the following disclaimer in the
  - documentation and/or other materials provided with the distribution.
  - Neither the name of the pig4cloud.com developer nor the names of its
  - contributors may be used to endorse or promote products derived from
  - this software without specific prior written permission.
  - Author: lengleng (wangiegie@gmail.com)
  -->
<template>
  <div class="execution z-h-100">
    <el-scrollbar ref="elScrollbar" class="scrollbarCls">
      <basic-container>
        <avue-crud
          ref="crud"
          :page="page"
          :data="tableData"
          :permission="permissionList"
          :table-loading="tableLoading"
          :option="tableOption"
          @on-load="getList"
          @search-change="searchChange"
          @refresh-change="refreshChange"
          @row-update="handleUpdate"
          @row-save="handleSave"
          @row-del="rowDel"
          @cell-mouse-enter="cellHover"
        >
          <!-- 自定义表单  -->
          <template slot="iconUrlForm">
            <div class="tenant_form_icon_wapper">
              <pt-upload-img
                class="tenant_form_icon"
                height="32px"
                width="32px"
                tip
                tip-size="12px"
                :img="img"
                @uploadCallback="uploadCallback"
              />
              <span class="icon_tip">请上传50X50的png格式</span>
            </div>
          </template>
          <!-- 自定义列 -->
          <template slot="iconUrl" slot-scope="scope">
            <div v-if="scope.row.iconUrl" class="tenant_colunm_icon">
              <img class="colunm_img" :src="scope.row.iconUrl">
            </div>
          </template>
        </avue-crud>
      </basic-container>
    </el-scrollbar>
  </div>
</template>

<script>
import { addObj, delObj, fetchList, putObj } from '@/api/admin/tenant'
import { tableOption } from '@/const/crud/admin/tenant'
import { mapGetters } from 'vuex'

export default {
  name: 'AuthorTenant',
  data() {
    return {
      searchForm: {},
      tableData: [],
      page: {
        total: 0, // 总页数
        currentPage: 1, // 当前页数
        pageSize: 10 // 每页显示多少条
      },
      tableLoading: false,
      tableOption: tableOption,
      img: {
        id: '',
        url: ''
      }
    }
  },
  computed: {
    ...mapGetters(['permissions']),
    permissionList() {
      return {
        addBtn: this.vaildData(this.permissions.admin_systenant_add, false),
        delBtn: this.vaildData(this.permissions.admin_systenant_del, false),
        editBtn: this.vaildData(this.permissions.admin_systenant_edit, false)
      }
    }
  },
  watch: {
    tableData() {
      this.$nextTick(() => {
        console.log(this.$refs.elScrollbar)
        this.$refs.elScrollbar.update()
      })
    }
  },
  mounted() {
    // 点击 新增的时候，清空img的信息
    const btn = document.querySelector('.ds-content .avue-crud__menu .el-button')
    btn.addEventListener('click', () => {
      this.fn_clearImg()
    })
  },
  methods: {
    getList(page, params) {
      this.tableLoading = true
      fetchList(Object.assign({
        current: page.currentPage,
        size: page.pageSize
      }, params, this.searchForm)).then(response => {
        this.tableData = response.data.data.records
        this.page.total = response.data.data.total
        this.tableLoading = false
      })['catch'](() => {
        this.tableLoading = false
      })
    },

    rowDel: function(row, index) {
      var _this = this
      this.$confirm('是否确认删除ID为' + row.id, '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(function() {
        return delObj(row.id)
      }).then(data => {
        _this.tableData.splice(index, 1)
        _this.$message({
          showClose: true,
          message: '删除成功',
          type: 'success'
        })
        this.getList(this.page)
      })
    },

    // 对操作列进行过滤
    cellHover(row, column, cell, event) {
      if (column.label !== '操作') {
        return
      }

      this.fn_clearImg(row.iconUrl, row.id)
    },

    // 数据更新
    /**
     * @param {object} row 为当前的数据
     * @param {String} index 为当前更新数据的行数
     * @param {object} done 为表单关闭函数
     * @param {object} loading 为表单关闭函数
     * @returns {Function} api
     **/
    handleUpdate: function(row, index, done, loading) {
      putObj({
        ...row,
        iconCode: this.img.id
      }).then(data => {
        this.tableData.splice(index, 1, Object.assign({}, row))
        this.$message({
          showClose: true,
          message: '修改成功',
          type: 'success'
        })
        done()
        this.getList(this.page)
        this.fn_clearImg()
      })['catch'](() => {
        loading()
      })
    },

    /**
     * @param {object} row 为当前的数据
     * @param {object} done 为表单关闭函数
     * @param {object} loading 为表单关闭函数
     * @returns {Function} api
     **/
    handleSave: function(row, done, loading) {
      addObj({
        ...row,
        iconCode: this.img.id
      }).then(data => {
        this.tableData.push(Object.assign({}, row))
        this.$message({
          showClose: true,
          message: '添加成功',
          type: 'success'
        })
        done()
        this.getList(this.page)
        this.fn_clearImg()
      })['catch'](() => {
        loading()
      })
    },

    /**
     * @param {object} form 为当前的数据搜索回调
     * @param {object} done 为当前的数据搜索回调
     * @returns {Function} api
     */
    searchChange(form, done) {
      done()
      this.searchForm = form
      this.getList(this.page, form)
    },

    /**
     * 刷新回调
     * @returns {Function} api
     */
    refreshChange() {
      this.getList(this.page)
    },
    // 上传文件的回调
    uploadCallback(res) {
      if (res.data.code === 0) {
        this.img = res.data.data
      }
    },
    // 清空img的数据
    fn_clearImg(url = '', id = '') {
      this.img.url = url
      this.img.id = id
    }

  }
}
</script>

<style lang="less" scoped>
.tenant_form_icon_wapper {
  display: flex;
  align-items: center;
  justify-content: flex-start;
  outline: none;
  height: 32px;
  width: 100%;
  & + .focusing {
    outline: none;
  }
  .tenant_form_icon {
    cursor: pointer;
    color: #c0c4cc;
  }
  div {
    padding: 0 10px;
    height: 100%;
    border: 1px solid #ddd;
    border-radius: 5px;
  }

  button {
    margin-left: 15px;
  }
  .avatar-uploader .el-upload {
    border: 1px dashed #d9d9d9;
    border-radius: 6px;
    cursor: pointer;
    position: relative;
    overflow: hidden;
  }
  .avatar-uploader .el-upload:hover {
    border-color: #409eff;
  }
  .avatar-uploader-icon {
    color: #8c939d;
    width: 100%;
    height: 100%;
    text-align: center;
  }
  .avatar {
    width: 50px;
    height: 50px;
    display: block;
  }

  // 上传图标的提示
  .icon_tip {
    display: inline-block;
    margin-left: 10px;
    color: #c0c4cc;
  }
}
.tenant_colunm_icon {
  display: flex;
  align-items: center;
  justify-content: center;
  position: absolute;
  top: 0;
  left: 0;
  height: 100%;
  width: 100%;
  .colunm_img {
    height: 40px;
    width: 40px;
  }
}
</style>
